<?php

namespace App\Http\Controllers;

use App\Framework\Enum\SinglePageIdEnum;
use App\Framework\Helper\TextHelper;
use App\Framework\Model\Message;
use App\Http\Requests\Game\GetActivityListForm;
use App\Http\Requests\Game\GetBroadcastListForm;
use App\Http\Requests\Game\GetCoinStatiListForm;
use App\Http\Requests\Game\GetExchangeRecordListForm;
use App\Http\Requests\Game\GetProductListForm;
use App\Http\Requests\Game\GetUserFeedbackForm;
use App\Http\Requests\Game\GetUserFeedbackListForm;
use App\Http\Requests\Game\StoreActivityForm;
use App\Http\Requests\Game\StoreBroadcastForm;
use App\Http\Requests\Game\StoreProductForm;
use App\Http\Requests\Game\UpdateActivityForm;
use App\Http\Requests\Game\UpdateExchangeRecordForm;
use App\Http\Requests\Game\UpdateProductForm;
use App\Http\Requests\Game\UpdateSinglePageForm;
use Illuminate\Http\Request;
use GameFacade;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\URL;

class GameController extends Controller
{
    //region 活动管理

    //活动列表
    public function getActivityList(GetActivityListForm $form)
    {
        $this->storeAdminLog('查询活动列表');
        $activities = GameFacade::getActivityList($form);
        return view('game.getActivityList')
            ->with('pagerSet', $activities)
            ->with('form', $form);
    }

    //新增活动
    public function addActivity()
    {
        $this->storeAdminLog('新增活动');
        return view('game.addActivity');
    }

    //保存活动
    public function storeActivity(StoreActivityForm $form)
    {
        $this->storeAdminLog('保存活动');
        $msg = GameFacade::addActivity($form);
        if ($msg->success) {
            return $this->showMessageAndRedirect('添加成功', URL::action('GameController@getActivityList'));
        }

        return $this->showMessageAndBack($msg->info);
    }

    //编辑活动
    public function editActivity($id)
    {
        $this->storeAdminLog('编辑活动');
        $activity = GameFacade::getActivityById($id);
        return view('game.editActivity')
            ->with('activity', $activity);
    }

    //修改活动
    public function updateActivity(UpdateActivityForm $form)
    {
        $this->storeAdminLog('修改活动');
        $msg = GameFacade::updateActivity($form);
        if ($msg->success) {
            return $this->showMessageAndRedirect('修改成功', URL::action('GameController@getActivityList'));
        }

        return $this->showMessageAndBack($msg->info);
    }

    //将活动排序序号向上移动
    public function setActivitySortUp(Request $request)
    {
        $this->storeAdminLog('将活动排序序号向上移动');
        $id = $request->get('id');
        $msg = GameFacade::setActivitySortUp($id);
        return response()->json($msg);
    }

    //将活动排序序号向下移动
    public function setActivitySortDown(Request $request)
    {
        $this->storeAdminLog('将活动排序序号向下移动');
        $id = $request->get('id');
        $msg = GameFacade::setActivitySortDown($id);
        return response()->json($msg);
    }

    //删除活动
    public function deleteActivity(Request $request)
    {
        $this->storeAdminLog('删除活动');
        $id = $request->get('id');
        $msg = GameFacade::deleteActivity($id);
        return response()->json($msg);
    }

    //批量删除商品
    public function batchDeleteActivity(Request $request)
    {
        $this->storeAdminLog('批量删除商品');
        $arr = $request->input('activityID');
        $ids = TextHelper::urlQueryToArray($arr, 'activityID');
        $msg = GameFacade::batchDeleteActivity($ids);
        return response()->json($msg);
    }

    //endregion

    //region 商品

    //查询商品
    public function getProductList(GetProductListForm $form)
    {
        $this->storeAdminLog('查询商品列表');
        $products = GameFacade::getProductList($form);
        return view('game.getProductList')
            ->with('pagerSet', $products)
            ->with('form', $form);
    }


    //新增商品
    public function addProduct()
    {
        $this->storeAdminLog('新增商品');
        return view('game.addProduct');
    }

    //保存商品
    public function storeProduct(StoreProductForm $form)
    {
        $this->storeAdminLog('保存商品');
        $msg = GameFacade::addProduct($form);
        if ($msg->success) {
            return $this->showMessageAndRedirect('添加成功', URL::action('GameController@getProductList'));
        }

        return $this->showMessageAndBack($msg->info);
    }

    //编辑活动
    public function editProduct($id)
    {
        $this->storeAdminLog('编辑活动');
        $product = GameFacade::getProductById($id);
        return view('game.editProduct')
            ->with('product', $product);
    }

    //修改活动
    public function updateProduct(UpdateProductForm $form)
    {
        $this->storeAdminLog('修改活动');
        $msg = GameFacade::updateProduct($form);
        if ($msg->success) {
            return $this->showMessageAndRedirect('修改成功', URL::action('GameController@getProductList'));
        }

        return $this->showMessageAndBack($msg->info);
    }

    //将活动排序序号向上移动
    public function setProductSortUp(Request $request)
    {
        $this->storeAdminLog('将活动排序序号向上移动');
        $id = $request->get('id');
        $msg = GameFacade::setProductSortUp($id);
        return response()->json($msg);
    }

    //将活动排序序号向下移动
    public function setProductSortDown(Request $request)
    {
        $this->storeAdminLog('将活动排序序号向下移动');
        $id = $request->get('id');
        $msg = GameFacade::setProductSortDown($id);
        return response()->json($msg);
    }

    //删除商品
    public function deleteProduct(Request $request)
    {
        $this->storeAdminLog('删除商品');
        $id = $request->get('id');
        $msg = GameFacade::deleteProduct($id);
        return response()->json($msg);
    }

    //批量删除商品
    public function batchDeleteProduct(Request $request)
    {
        $this->storeAdminLog('批量删除商品');
        $arr = $request->input('productID');
        $ids = TextHelper::urlQueryToArray($arr, 'productID');
        $msg = GameFacade::batchDeleteProduct($ids);
        return response()->json($msg);
    }

    //endregion

    //region 兑换记录

    //查询兑换记录列表
    public function getExchangeRecordList(GetExchangeRecordListForm $form)
    {
        $this->storeAdminLog('查询兑换记录列表');
        $records = GameFacade::getExchangeRecordList($form);
        return view('game.getExchangeRecordList')
            ->with('pagerSet', $records)
            ->with('form', $form);
    }

    //查询兑换详情
    public function getExchangeRecordDetail($id)
    {
        $this->storeAdminLog('查询兑换详情');
        $record = GameFacade::getExchangeRecord($id);
        return view('game.getExchangeRecordDetail')
            ->with('record', $record);
    }

    //修改兑换记录
    public function updateExchangeRecord(UpdateExchangeRecordForm $form)
    {
        $this->storeAdminLog('修改兑换记录');
        $msg = GameFacade::updateExchangeRecord($form);
        if ($msg->success) {
            return $this->showMessageAndRedirect('修改成功', URL::action('GameController@getExchangeRecordList'));
        }

        return $this->showMessageAndBack($msg->info);
    }

    //修改兑换记录状态
    public function setExchangeRecordStatus(Request $request)
    {
        $this->storeAdminLog('修改兑换记录状');
        $recordID = Input::get('recordID');
        $status = Input::get('status');
        $ids = TextHelper::urlQueryToArray($recordID, 'recordID');
        $msg = GameFacade::setExchangeRecordStatus($ids, $status);
        return response()->json($msg);
    }

    //endregion


    //获取单页
    public function getSinglePage()
    {
        $this->storeAdminLog('获取单页');
        $id = Input::get('id');
        if (empty($id)) {
            $id = SinglePageIdEnum::PLAYER_RULE;
        }

        $pageList = GameFacade::getSinlePageList();
        return view('game.getSinglePage')
            ->with('pageList', $pageList)
            ->with('id', $id);
    }

    //修改单页
    public function updateSinglePage(UpdateSinglePageForm $form)
    {
        $this->storeAdminLog('修改单页');
        $msg = GameFacade::updateSinglePage($form);
        if ($msg->success) {
            return $this->showMessageAndRedirect('修改成功', URL::action('GameController@getSinglePage', ['id' => $form->id]));
        }

        return $this->showMessageAndBack($msg->info);
    }

    //查询用户反馈
    public function getUserFeedbackList(GetUserFeedbackListForm $form)
    {
        $this->storeAdminLog('查询用户反馈');
        $list = GameFacade::getUserFeedbackList($form);
        return view('game.getUserFeedbackList')
            ->with('pagerSet', $list)
            ->with('form', $form);
    }

    //查看用户反馈详情
    public function getUserFeedbackDetail(Request $request)
    {
        $this->storeAdminLog('查看用户反馈详情');
        $id = $request->get('id');
        $feed = GameFacade::getUserFeedback($id);
        return view('game.getUserFeedbackDetail')
            ->with('feed', $feed);
    }

    //删除用户反馈
    public function deleteUserFeedback(Request $request)
    {
        $this->storeAdminLog('删除用户反馈');
        $id = $request->get('id');
        $msg = GameFacade::deleteUserFeedback($id);
        return response()->json($msg);
    }

    //获取站内信列表
    public function getBroadcastList(GetBroadcastListForm $form)
    {
        $this->storeAdminLog('获取站内信列表');
        $list = GameFacade::getBroadcastList($form);
        return view('game.getBroadcastList')
            ->with('pagerSet', $list)
            ->with('form', $form);
    }

    //发送站内信
    public function addBroadcast()
    {
        $this->storeAdminLog('发送站内信');
        return view('game.addBroadcast');
    }

    //保存并推送站内信
    public function storeBroadcast(StoreBroadcastForm $form)
    {
        $this->storeAdminLog('保存并推送站内信');
        $msg = GameFacade::addBroadcast($form);
        if ($msg->success) {
            return $this->showMessageAndRedirect('发送成功', URL::action('GameController@addBroadcast'));
        }

        return $this->showMessageAndBack($msg->info);
    }

    //删除站内信
    public function deleteBroadcast(Request $request)
    {
        $this->storeAdminLog('删除站内信');
        $id = $request->get('id');
        $msg = GameFacade::deleteBroadcast($id);
        return response()->json($msg);
    }

    //获取金币汇总列表
    public function getCoinStatiList(GetCoinStatiListForm $form)
    {
        $this->storeAdminLog('获取金币汇总列表');
        $list = GameFacade::getCoinStatiList($form);
        return view('game.getCoinStatiList')
            ->with('pagerSet', $list)
            ->with('form', $form);
    }
}
